package com.woniuxy.shior;

import lombok.var;
import org.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilter;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class JWTFilter extends BasicHttpAuthenticationFilter {
    @Override
    protected boolean preHandle(ServletRequest request, ServletResponse response) throws Exception {
        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        HttpServletResponse httpServletResponse = (HttpServletResponse) response;
        httpServletResponse.setHeader("Access-control-Allow-Origin", httpServletRequest.getHeader("Origin"));
        httpServletResponse.setHeader("Access-Control-Allow-Methods", "GET,POST,OPTIONS,PUT,DELETE");
        httpServletResponse.setHeader("Access-Control-Allow-Headers", httpServletRequest.getHeader("Access-Control-Request-Headers"));
        // 跨域时会首先发送一个option请求，这里我们给option请求直接返回正常状态
        if (httpServletRequest.getMethod().equals(RequestMethod.OPTIONS.name())) {
            httpServletResponse.setStatus(HttpStatus.OK.value());
            return false;
        }
        return super.preHandle(request, response);
    }

    /**
     * 登录认证
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @Override
    protected boolean executeLogin(ServletRequest request, ServletResponse response) throws Exception {
        HttpServletRequest request1 =  (HttpServletRequest) request;
        String token = request1.getHeader("token");
        getSubject(request,response).login(new MyJWt(token));
        return true;
    }

    /**
     * 登录请求判断，尝试获取请求头中的token信息！
     * @param request  incoming ServletRequest
     * @param response outgoing ServletResponse
     * @return true if the incoming request is an attempt to log in based, false otherwise
     */
    @Override
    protected boolean isLoginAttempt(ServletRequest request, ServletResponse response) {
        var request1 = (HttpServletRequest) request;
        String token = request1.getHeader("token");
        return token!=null;
    }

    /**
     * 如果请求头没有token则跳转401请求controller
     * @param request
     * @param response
     */
    private void response401(ServletRequest request,ServletResponse response){
        HttpServletResponse response1 = (HttpServletResponse) response;
//        try {
//            response1.sendRedirect("/401");
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
    }

    /**
     * token认证入口，进行业务逻辑处理！和后续过滤器方法的调用
     * @param request     The current HTTP servlet request.
     * @param response    The current HTTP servlet response.
     * @param mappedValue The array of configured HTTP methods as strings. This is empty if no methods are configured.
     */
    @Override
    protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) {
        if(isLoginAttempt(request,response)){
            try {
                executeLogin(request, response);
            } catch (Exception e) {
                response401(request,response);
                e.printStackTrace();
            }
        }else{
            response401(request,response);
        }
        return true;
    }
}
